*Table_7
do Define_Globals

*****************************************************************
* Table 7 - Linear Treatment Effects for a 100 percantage points 
* cap increase. To get the coefficeints presented inthe table 
* the coefficents obtained in the regression below should be divided
* by 10
*****************************************************************
use listing_final, clear

gen treatment = 0.36 - max_rate
gen sub_prime = (prime_categ == 3)

**** Second Differences 
gen after_super_prime = after_change * super_prime
gen treatment_after_change = treatment * after_change
gen treatment_sub_prime = treatment * sub_prime
gen treatment_prime = treatment * prime
gen treatment_super_prime = treatment * super_prime

**** Third Differences 
gen after_Treatment_sub_prime = treatment_after_change * sub_prime
gen after_Treatment_prime = treatment_after_change * prime
gen after_Treatment_super_prime = treatment_after_change * super_prime


* Need to drop reference groups in each of the differences to avoid multicollinearity 
* For consistency, whenever a difference include risk type, I drop the super prime element 
drop super_prime treatment_super_prime after_super_prime treatment_after_change 

*********************************
* Funding probability
*********************************

global DIFF1 "super_prime prime treat_1 treat_2 treat_3 after_change"
global DIFF2 "treat_1_sub_prime treat_2_sub_prime treat_3_sub_prime treat_1_prime treat_2_prime treat_3_prime after_prime after_sub_prime"
global DIFF3 "after_Treat_1_super_prime after_Treat_1_prime after_Treat_1_sub_prime after_Treat_2_super_prime after_Treat_2_prime after_Treat_2_sub_prime after_Treat_3_super_prime after_Treat_3_prime after_Treat_3_sub_prime"
global COVARIATES "amount_1  amount_2 amount_3 amount_4 auction_open_for_duration home amountdelinquent publicrecordslast10years publicrecordslast12months revolvingcreditbalance totalcreditlines inquiries_1 inquiries_2 delinq7_1 delinq7_2 current_delinq_1 current_delinq_2 bankcardutilization_1 bankcardutilization_2 current_lines_1 current_lines_2 current_lines_3 current_lines_4 key_word_* category_dummy_* income_dummy_* employment_dummy_* st_dum_* " 
global WEEK_DUM " week_dum_* "


dprobit funded ///
	after_Treatment_super_prime after_Treatment_prime after_Treatment_sub_prime ///
	after_prime after_sub_prime ///
	treatment_prime treatment_sub_prime ///
	treatment after_change sub_prime prime ///
	$COVARIATES $WEEK_DUM, ///
	vce(cluster st_week)
	

********************************
* Amount Requested
********************************
gen cens =  0 
replace cens = -1 if (amountrequested == 1000)
replace cens = 1 if (amountrequested == 25000)
	
cnreg log_amount_req /// 
	after_Treatment_super_prime after_Treatment_prime after_Treatment_sub_prime ///
	after_prime after_sub_prime ///
	treatment_prime treatment_sub_prime ///
	treatment after_change sub_prime prime ///
	$COVARIATES $WEEK_DUM, ///
	vce(cluster st_week) censored(cens) 

*Calculate marginal effect
local log_1000 =  log(1000)
local log_25000 = log(25000)
mfx compute, predict(e(`log_1000',`log_25000')) varlist(after_Treatment_super_prime after_Treatment_prime after_Treatment_sub_prime) force

******************************
* APR
******************************
drop cens 
egen st_num = group(st)

*remove loans which from some reason have a borrowerrate > true_max_rate
drop if funded & borrowerrate > true_max_rate

*For all listings that were funded, set the borrowerrate in the max_rate (as we know 
*that they were not funded because the equilibrium rate is higher than the cap)
gen borrowerrate_tobit = borrowerrate
replace borrowerrate_tobit = true_max_rate if funded == 0

gen cens = 0 
replace cens = 1 if (~funded | /// 
				     (funded & borrowerrate_tobit == true_max_rate))

*Find the funding based on the estimates of the before period 
probit funded /// 
	treatment_prime treatment_sub_prime ///
	treatment sub_prime prime ///
	amount_1 amount_2 amount_3 amount_4 /// 
    auction_open_for_duration home /// 
   	amountdelinquent  ///   
 	publicrecordslast10years /// 
	publicrecordslast12months /// 
	revolvingcreditbalance totalcreditlines ///
	inquiries_1 inquiries_2 ///
	delinq7_1 delinq7_2 ///	       
	current_delinq_1 current_delinq_2 ///
	bankcardutilization_1 bankcardutilization_2 ///
	current_lines_1 current_lines_2 current_lines_3 current_lines_4 ///
	key_word_* category_dummy_* income_dummy_* /// 
	employment_dummy_* ///
	st_dum_* if (~after_change), ///  
	vce(cluster st_num)

*Predict the funding probability for the full sample	
predict pr_fund

cnreg borrowerrate_tobit /// 
after_Treatment_super_prime after_Treatment_prime after_Treatment_sub_prime ///
after_prime after_sub_prime ///
treatment_prime treatment_sub_prime ///
treatment after_change sub_prime prime ///
$COVARIATES $WEEK_DUM if (pr_fund >= 0.1) ,  ///
vce(cluster st_week) censored(cens)

test after_Treatment_super_prime after_Treatment_prime after_Treatment_sub_prime
mfx compute, predict(e(0,true_max_rate)) ///
	varlist(after_Treatment_super_prime after_Treatment_prime after_Treatment_sub_prime) force

	
************************************
* Default 
************************************
*First stage regression
dprobit funded ///
	after_Treatment_super_prime after_Treatment_prime after_Treatment_sub_prime ///
	after_prime after_sub_prime ///
	treatment_prime treatment_sub_prime ///
	treatment after_change sub_prime prime ///
	$COVARIATES $WEEK_DUM, ///
	vce(cluster st_week)
matrix V0 = e(V)
predict double zhat 
keep listingkey zhat
sort listingkey
save listingkey_pred_fund_prob_linear_effects_3_risk_levels, replace


use listing_final, clear
sort listingkey
merge listingkey using listingkey_pred_fund_prob_linear_effects_3_risk_levels
tab _merge
keep if _merge == 3
drop _merge

**** First Differences 
gen treatment = 0.36 - max_rate
gen sub_prime = (prime_categ == 3)

**** Second Differences 
gen after_super_prime = after_change * super_prime
gen treatment_after_change = treatment * after_change
gen treatment_sub_prime = treatment * sub_prime
gen treatment_prime = treatment * prime
gen treatment_super_prime = treatment * super_prime

**** Third Differences 
gen after_Treatment_sub_prime = treatment_after_change * sub_prime
gen after_Treatment_prime = treatment_after_change * prime
gen after_Treatment_super_prime = treatment_after_change * super_prime


* Need to drop reference groups in each of the differences to avoid multicollinearity 
* For consistency, whenever a difference include risk type, I drop the super prime element 
drop super_prime treatment_super_prime after_super_prime treatment_after_change 

global DIFF1 "treatment after_change sub_prime prime"
global DIFF2 "after_prime after_sub_prime treatment_prime treatment_sub_prime "
global DIFF3 "after_Treatment_super_prime after_Treatment_prime after_Treatment_sub_prime"
global COVARIATES "amount_1  amount_2 amount_3 amount_4 auction_open_for_duration home amountdelinquent publicrecordslast10years publicrecordslast12months revolvingcreditbalance totalcreditlines inquiries_1 inquiries_2 delinq7_1 delinq7_2 current_delinq_1 current_delinq_2 bankcardutilization_1 bankcardutilization_2 current_lines_1 current_lines_2 current_lines_3 current_lines_4 key_word_* category_dummy_* income_dummy_* employment_dummy_* st_dum_* " 
global WEEK_DUM " week_dum_* "

gen byte cons = 1

*Second stage regression for the repayment varibles
reg default_18 /// 
	zhat $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM, vce(cluster st_week)

*A joint test fo the null hypothesis that all treatment effects are 0			
test $DIFF3
  
matrix V4 = e(V)
predict double yhat
scalar zz = _b[zhat]

matrix accum C = $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM cons zhat ///
			 $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM cons /// 
			 [iw = (`var' - yhat)*(`var' - yhat)*zhat*(1-zhat)*zz], nocons
			 
matrix accum R = $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM cons zhat ///
			 $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM cons ///
			 [iw = (`var' - yhat)*(`var'-zhat)], nocons			     

matrix C = C[146..291,1..145]
matrix R = R[146..291,1..145]	

matrix M = V4 + (V4*(C*V0*C' - R*V0*C' - C*V0*R')*V4)
matrix sub_mat = M[2..4,2..4]
matrix diag = vecdiag(sub_mat)

*In order to find the "true" std one has to take the square root of each entry of the vector
*It seems that the increase in the standard error is almost non-existant

*The is the corrected variance matrix
matrix list diag
